package com.qzimp.fujiancuo.controller;

import com.qzimp.fujiancuo.entity.Email;
import com.qzimp.fujiancuo.entity.vo.ResultCode;
import com.qzimp.fujiancuo.entity.vo.ResultVo;
import com.qzimp.fujiancuo.exceptions.ApiException;
import com.qzimp.fujiancuo.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/email")
public class EmailController {

    @Autowired
    private EmailService emailService;

@GetMapping("/sendValidateCode")
public ResultVo sendValidateCode(String mail){

    if(StringUtils.isEmpty(mail)){
        throw  new ApiException(ResultCode.EXCEPTION_ERROR,"邮件接收者不能为空");
    }

    try {
        Email email=new Email();
        email.setUser(mail);


        email.setSubject("邮箱验证码");

        email.setContent("2303840446黄智伟");

        emailService.sendMail(email);
        return new ResultVo(ResultCode.SUCCESS);
    }
    catch (Exception e){

        throw  new ApiException(ResultCode.EXCEPTION_ERROR,"邮件发送失败");
    }

}
//邮箱验证码
    @PostMapping("/sendCode")
    public void sendMessage(String address,String content){
        {
            Email email = new Email();
            email.setUser(address);
            email.setSubject("邮箱验证码");
            email.setContent("2303840446黄智伟"+
                    "验证码为"+content);
            emailService.sendMail(email);
        }
    }
}
